package one.stand.controller.supplier;

import com.jmp.api.dto.*;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.*;
import com.jmp.feign.WebsiteFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.model.ResultModel;
import one.stand.model.Website;
import one.stand.service.WebsiteService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cjr
 */
@Api(description = "官网管理")
@RequestMapping("website")
@RestController
public class WebsiteController {

    @Autowired
    private WebsiteFeign websiteFeign;

    @Autowired
    private WebsiteService websiteService;

    @ApiOperation(value = "1官网信息")
    @GetMapping("")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<WebsiteResponse> info() {
        final Integer userCompanyId = RequestContextHolder.getRequestContext().getUser().getCompanyId();

        Website website = websiteService.getByCompanyId(userCompanyId);

        ResultModel<WebsiteVideoVo> resultModel = websiteFeign.video(WebsiteVideoDto.builder().companyId(userCompanyId).build());
        AssertUtil.isTrue(resultModel);
        List<WebsiteResponse.Video> videoModels = ConvertUtils.list(resultModel.getData().getVideoList(), video -> {
            return WebsiteResponse.Video.builder()
                    .videoId(video.getVideoId())
                    .url(video.getUrl())
                    .describes(video.getDescribes())
                    .cover(video.getCover())
                    .build();
        });

        WebsiteDemandDto demandDto = WebsiteDemandDto.builder().companyId(userCompanyId).build();
        demandDto.setPageNum(1);
        demandDto.setPageSize(1);
        ResultModel<WebsiteDemandVo> demandVoResultModel = websiteFeign.demand(demandDto);
        AssertUtil.isTrue(demandVoResultModel);
        Long num = demandVoResultModel.getData().getDemandPage().getTotal();

        WebsiteResponse response = WebsiteResponse.builder()
                .photos(website.getPhotos())
                .companyIntroduce(website.getCompanyIntroduce())
                .goodsIntroduce(website.getGoodsIntroduce())
                .videos(videoModels)
                .num(num)
                .phone(website.getPhone())
                .videos(videoModels)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "2官网保存(图片|企业介绍|产品介绍|联系电话)")
    @PostMapping("save")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel save(@RequestAttribute WebsiteSaveRequest request) {
        final Integer userCompanyId = RequestContextHolder.getRequestContext().getUser().getCompanyId();

        Website website = websiteService.getByCompanyId(userCompanyId);

        Website websiteUpdate = new Website();
        websiteUpdate.setWebsiteId(website.getWebsiteId());
        websiteUpdate.setPhotos(request.getPhotos());
        websiteUpdate.setCompanyIntroduce(request.getCompanyIntroduce());
        websiteUpdate.setGoodsIntroduce(request.getGoodsIntroduce());
        websiteUpdate.setPhone(request.getPhone());
        websiteService.update(websiteUpdate);

        return ResultModel.success();
    }

    @ApiOperation(value = "3视频：新增|修改")
    @PostMapping("video/save")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel videoSave(@RequestAttribute WebsiteVideoSaveRequest request) {

        WebsiteVideoSaveDto saveDto = WebsiteVideoSaveDto.builder()
                .videoId(request.getVideoId())
                .url(request.getUrl())
                .describes(request.getDescribes())
                .cover(request.getCover())
                .build();
        ResultModel resultModel = websiteFeign.videoSave(saveDto);
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @ApiOperation(value = "3视频：删除")
    @PostMapping("video/remove")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel videoRemove(@RequestAttribute WebsiteVideRemoveRequest request) {

        ResultModel resultModel = websiteFeign.videoRemove(WebsiteVideoRemoveDto.builder().videoId(request.getVideoId()).build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @ApiOperation(value = "3视频：上下移动")
    @PostMapping("video/move")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel videoMove(@RequestAttribute WebsiteVideoMoveRequest request) {

        ResultModel resultModel = websiteFeign.videoMove(WebsiteVideoMoveDto.builder()
                .videoId(request.getVideoId())
                .isUp(request.getType() == 1)
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @ApiOperation(value = "4表单数据列表(分页|需求）")
    @PostMapping("demand")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<WebsiteDemandResponse> demand(@RequestAttribute WebsiteDemandRequest request) {
        final Integer optCompanyId = RequestContextHolder.getRequestContext().getUser().getCompanyId();

        WebsiteDemandDto demandDto = WebsiteDemandDto.builder().companyId(optCompanyId).build();
        demandDto.setPageNum(request.getPageNum());
        demandDto.setPageSize(request.getPageSize());
        ResultModel<WebsiteDemandVo> resultModel = websiteFeign.demand(demandDto);
        AssertUtil.isTrue(resultModel);

        PageResponse2<WebsiteDemandResponse.Demand> demandPageResponse2 = ConvertUtils.page2(resultModel.getData().getDemandPage(), demand -> {
            return WebsiteDemandResponse.Demand.builder()
                    .logo(demand.getLog())
                    .name(demand.getName())
                    .phone(demand.getPhone())
                    .text(demand.getText())
                    .build();
        });

        WebsiteDemandResponse response = WebsiteDemandResponse.builder()
                .demandPage(demandPageResponse2)
                .build();
        return ResultModel.success(response);
    }
}
